<template>
	<el-dialog v-model="_d.visible" draggable :close-on-click-modal="false" width="600px">
		<template #header>
			<div style="color: #fff">{{_d.record ? '编辑' : '添加'}}采购方式</div>
		</template>
	  <el-form class="form-box" :model="_d.form" ref="formRef" :rules="_d.rules" label-width="auto">
			<el-form-item label="名称" prop="name">
				<el-input v-model="_d.form.name" placeholder="请输入" clearable />
			</el-form-item>
			<el-form-item label="描述" prop="description">
				<el-input v-model="_d.form.description" type="textarea" :rows="6" placeholder="请输入" clearable />
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="cancel">关 闭</el-button>
				<el-button :loading="_d.btnLoading" type="primary" @click="save">确 认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, computed, onMounted } from 'vue';
import { ElNotification } from 'element-plus';
import {verifyPhone, verifyTelPhone} from "/@/utils/toolsValidate.ts";
import {$get, $post} from '/@/utils/request';
import {useUserInfo} from "/@/stores/userInfo.ts";


const emits = defineEmits(['reload']);

const oUserInfo = useUserInfo();
const formRef = ref();

const _d = reactive({
	visible: false,
	btnLoading: false,

	record: null,

	form: {
		name: '',
		description: '',
	},
	
	rules: {
		name: [
			{ required: true, message: '采购方式名称不能为空', trigger: 'blur' }
		],
	},
});

onMounted(() => {
	
});

// 打开弹窗
const open = (record) => {
	_d.record = record || null;
	_d.form.name = record ? record.name : '';
	_d.form.description = record ? record.description : '';
	formRef.value?.clearValidate();

	_d.visible = true;
};

// 取消
const cancel = () => {
	_d.visible = false;
};

// 保存
const save = () => {
	formRef.value.validate(async (valid) => {
		if (!valid) return;
		if(!_d.record) {
			addData();
		} else {
			editData();
		}
	})
}

const addData = () => {
	let params = {
		name: _d.form.name,
		description: _d.form.description,
	}
	_d.btnLoading = true;
	$post('/projectcfgsrv/inspurchase', params).then(res => {
		if(res.code == 200) {
			ElNotification({
		    title: '提示',
		    message: '添加成功',
		    type: 'success',
		  })
			emits('reload');
			_d.visible = false;
		}
	}).finally(() => {
		_d.btnLoading = false;
	})
}

const editData = () => {
	let params = {
		id: _d.record.id,
		name: _d.form.name,
		description: _d.form.description,
	}
	_d.btnLoading = true;
	$post('/projectcfgsrv/modifypurchase', params).then(res => {
		if(res.code == 200) {
			ElNotification({
		    title: '提示',
		    message: '编辑成功',
		    type: 'success',
		  })
			emits('reload');
			_d.visible = false;
		}
	}).finally(() => {
		_d.btnLoading = false;
	})
}

// 导出对象
defineExpose({ open });

</script>
<style lang="scss" scoped>
.form-box {
	min-height: 250px;
}
</style>
